#!/bin/bash
# 此脚本用来编译安装rMATS-4.1.2软件
# rMATS-4.1.2软件介绍
#   rMATs是检测可变剪切事件的常用软件之一，它可以从RNA测序数据中，检测出多种类型的可变剪切事件，
#   并提供了定量和组间差异分析的功能，可对生物学重复的样本进行组间分析。
#  Usage:
#     bash /path/to/build.sh /path/to/build /path/to/install
# 使用build.sh 脚本样例 bash /path/to/build.sh /path/to/build /path/to/install
# 使用build.sh 需要root权限或者sudo权限,否则无法安装依赖
#
##############################################################

source "$(
  cd "$(dirname "$0")" || exit 1
  pwd
)/common.sh"

function install_development() {
  sudo yum install -y m4 bc zlib-devel zlib environment-modules systemd-devel
  source /etc/profile
  module purge
}

function install_bisheng() {
  bash "$1"/bisheng-compiler-2.1.0.sh "$2" "$3"
  module use "$3"/bisheng-compiler-2.1.0
  module load "$3"/bisheng-compiler-2.1.0/bisheng_modulefiles
}

function install_gsl() {
  bash "$1"/gsl-2.5.sh "$2" "$3"
  module use "$3"/gsl-2.5
  module load "$3"/gsl-2.5/gsl_modulefiles
}

function install_lapack() {
  bash "$1"/lapack-3.10.1.sh "$2" "$3"
  module use "$3"/lapack-3.10.1
  module load "$3"/lapack-3.10.1/lapack_modulefiles
}

function install_R() {
  bash "$1"/R-4.2.0.sh "$2" "$3"
  module use "$3"/R-4.2.0
  module load "$3"/R-4.2.0/R-4.2.0_modulefiles
}

function install_STAR() {
  bash "$1"/STAR-2.7.10a.sh "$2" "$3"
  module use "$3"/STAR-2.7.10a
  module load "$3"/STAR-2.7.10a/STAR-2.7.10a_modulefiles
}

function install_rmats() {
  bash "$4"/rmats-4.1.2.sh "$1" "$2"
  mkdir -p "$3"/rmats-4.1.2-hpc && cp -r "$2"/* "$3"/rmats-4.1.2-hpc/
}

function copy_common_and_R_shell() {
  rm -rf "$2"/rmats-4.1.2-hpc/R-4.2.0
  cp "$1"/dependency/R-4.2.0.sh "$1"/common.sh "$1"/dependency/pairadise.sh "$1"/dependency/install-R-deps.R "$2"/rmats-4.1.2-hpc/
  sed -i '6 c\)/common.sh"' "$2"/rmats-4.1.2-hpc/R-4.2.0.sh
}

function write_rmats_run() {
  cat >"$1/rmats-4.1.2-hpc/run.sh" <<EOF
#!/bin/bash
yum install -y wget tar git environment-modules cmake m4 bc zlib-devel zlib \\
systemd-devel patch Cython samtools libatomic glibc-headers gcc-c++ readline-devel \\
libXt-devel bzip2-devel xz-devel python python2-numpy* libcurl-devel pcre*

source /etc/profile &>/dev/null

current_dir="\$(cd "\$(dirname "\${BASH_SOURCE[0]}")" && pwd -P)"
for f in "\${current_dir}"/*; do
  if [[ -d "\${f}" && -f "\$(ls "\${f}"/*modulefiles 2>&1)" ]]; then
    module use "\${f}" && module load "\${f}"/*modulefiles
  fi
done

# 编译 R 并初始化其环境变量
mkdir "\${current_dir}"/build
bash "\${current_dir}"/R-4.2.0.sh "\${current_dir}"/build "\${current_dir}"
module use "\${current_dir}"/R-4.2.0
module load "\${current_dir}"/R-4.2.0/R-4.2.0_modulefiles
rm -rf "\${current_dir}"/build

# 安装PAIRADISE 安装PAIRADISE时，需要下载nloptr等依赖，如果运行run.sh下载依赖失败，请多执行几次
bash "\${current_dir}"/pairadise.sh "\${current_dir}"

echo -e "\033[1;32;1mrMATS environment initialization completed.\033[0m"

EOF

  chmod +x "$1"/rmats-4.1.2-hpc/run.sh
}

function tar_pkg() {
  echo "# 正在打包 rmats 文件 路径为$1"
  cd "$1" && tar -zcf rmats-4.1.2-hpc.tar.gz rmats-4.1.2-hpc
  echo -e "\033[1;32;1m# rmats 已经打包成功 文件路径为$1/rmats-4.1.2-hpc.tar.gz\033[0m"
}

function main() {
  set_script_dir

  # $1（构建路径）、$2（安装路径）检测
  is_check_build_install_directory "$1" "$2" "build.sh"

  # 构建路径
  buildpath=$(create_dir "$1")

  # 安装路径
  installpath=$(create_dir "$2")

  # 依赖路径
  dep_dir="${SCRIPT_DIR}"/dependency

  # 打包路径
  pkg_dir="${SCRIPT_DIR}"/pkg

  install_development
  install_bisheng "$dep_dir" "$buildpath" "$installpath"
  install_gsl "$dep_dir" "$buildpath" "$installpath"
  install_lapack "$dep_dir" "$buildpath" "$installpath"
  install_R "$dep_dir" "$buildpath" "$installpath"
  install_STAR "$dep_dir" "$buildpath" "$installpath"
  install_rmats "$buildpath" "$installpath" "$pkg_dir" "${SCRIPT_DIR}"
  set -e
  copy_common_and_R_shell "${SCRIPT_DIR}" "$pkg_dir"
  write_rmats_run "$pkg_dir"
  tar_pkg "$pkg_dir"
  echo " # ------------------------------------------------------------------------"
  echo "# 运行软件前请执行以下命令"
  echo -e "\033[1;32;1m# source ""${pkg_dir}""/rmats-4.1.2-hpc/run.sh\033[0m"
  echo "# 来加载 rmats 运行时环境变量"
  echo "# ------------------------------------------------------------------------"
  rm -rf "$buildpath" "$installpath"
}

main "$@"
